package com.aaa.sys.controller;

import com.aaa.common.util.BaseUtil;
import com.aaa.common.util.Result;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.TreeNode;
import com.aaa.sys.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @FileName: MenuController
 * @Description:
 * @Author: zhz
 * @CreateTime: 2025/8/29 11:59
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private HttpServletRequest request;
    /**
     * 添加菜单
     * @param treeNode 菜单树节点对象，包含菜单的相关信息
     * @return 添加结果，包含添加的菜单信息
     */
    @PostMapping("add")
    public Result addMenu(@RequestBody TreeNode treeNode) {
        return BaseUtil.success(menuService.addMenu(treeNode));
    }

    /**
     * 更新菜单
     * @param treeNode 菜单树节点对象，包含要更新的菜单信息
     * @return 更新结果，包含更新后的菜单信息
     */
    @PostMapping("update")
    public Result updateMenu(@RequestBody TreeNode treeNode) {
        return BaseUtil.success( menuService.updateMenu(treeNode));
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return 删除结果，包含删除影响的记录数
     */
    @DeleteMapping("delete")
    public Result deleteMenu(Integer id) {
        return BaseUtil.success( menuService.deleteMenu(id));
    }


    /**
     * 查询所有树数据
     * @return
     */
    @GetMapping("queryAllTreeData")
    public Result queryAllTreeData(){
        return BaseUtil.success(menuService.queryTreeData());
    }
    /**
     * 根据用户编号查询树数据
     * @return
     */
    @GetMapping("queryTreeDataByUserId")
    public Result  queryTreeDataByUserId(){
        String userId = TokenUtil.getUserId(request);
        return BaseUtil.success(menuService.queryTreeDataByUserId(Integer.parseInt(userId)));
    }

    /**
     * 据当前登录的用户编号，查询该用户拥有的按钮权限
     * @return
     */
    @GetMapping("queryButtonPermsByUserId")
    public Result queryButtonPermsByUserId(){
        return BaseUtil.success(menuService.queryButtonPermsByUserId());
    }
}
